package cn.flying.cloud.system.log.service.impl;

import java.io.Serializable;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.flying.cloud.base.common.utils.Ft;
import cn.flying.cloud.base.common.vo.PageRequest;
import cn.flying.cloud.base.common.vo.PageResponse;
import cn.flying.cloud.system.log.entity.SysSeqInfo;
import cn.flying.cloud.system.log.mapper.SysSeqInfoMapper;
import cn.flying.cloud.system.log.service.SysSeqInfoService;

/**
 * <p>
 * 序列号表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-04-02 13:08:18
 */
@Service
public class SysSeqInfoServiceImpl extends ServiceImpl<SysSeqInfoMapper, SysSeqInfo> implements SysSeqInfoService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 分页查询序列号表数据
     *
     * @param request
     * @return
     */
    @Override
    public PageResponse getPageList(PageRequest<SysSeqInfo> request) {
        SysSeqInfo param = request.getParam();
        LambdaQueryWrapper<SysSeqInfo> wrapper = getLambdaQueryWrapper(param);
        Page<SysSeqInfo> page = this.page(new Page<>(request.getPageNum(), request.getPageSize()), wrapper);
        return new PageResponse(page.getTotal(), (int) page.getCurrent(), page.getRecords());
    }

    /**
     * 构建查询对象
     *
     * @param param
     * @return
     */
    private LambdaQueryWrapper<SysSeqInfo> getLambdaQueryWrapper(SysSeqInfo param) {
        LambdaQueryWrapper<SysSeqInfo> wrapper = new LambdaQueryWrapper<>();
        if (param != null) {
            if (Ft.String.isNotBlank(param.getSeqType())) {
                wrapper.eq(SysSeqInfo::getSeqType, param.getSeqType());
            }
            if (Ft.String.isNotBlank(param.getSeqDesc())) {
                wrapper.like(SysSeqInfo::getSeqDesc, "%" + param.getSeqDesc() + "%");
            }
            if (Ft.String.isNotBlank(param.getStatus())) {
                wrapper.eq(SysSeqInfo::getStatus, param.getStatus());
            }
        }
        return wrapper;
    }

    /**
     * 列表查询序列号表数据
     *
     * @param sysSeqInfo
     * @return
     */
    @Override
    public List<SysSeqInfo> getList(SysSeqInfo sysSeqInfo) {
        LambdaQueryWrapper<SysSeqInfo> wrapper = getLambdaQueryWrapper(sysSeqInfo);
        List<SysSeqInfo> list = this.list(wrapper);
        return list;
    }

    /**
     * 查询序列号表详情数据
     *
     * @param sysSeqInfo
     * @return
     */
    @Override
    public SysSeqInfo getOneObj(SysSeqInfo sysSeqInfo) {
        LambdaQueryWrapper<SysSeqInfo> wrapper = getLambdaQueryWrapper(sysSeqInfo);
        return this.getOne(wrapper);
    }


    /**
     * 新增序列号表数据
     *
     * @param sysSeqInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean createObj(SysSeqInfo sysSeqInfo) {
        return this.save(sysSeqInfo);
    }

    /**
     * 更新序列号表数据
     *
     * @param sysSeqInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateObj(SysSeqInfo sysSeqInfo) {
        return this.saveOrUpdate(sysSeqInfo);
    }

    /**
     * 删除序列号表数据
     *
     * @param sysSeqInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteObj(SysSeqInfo sysSeqInfo) {
        return this.removeById(sysSeqInfo.getId());
    }

    /**
     * 根据Id删除
     *
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteById(Serializable id) {
        return this.removeById(id);
    }

    /**
     * 根据类型获取下个序号
     *
     * @param type
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public String getNextSeq(String type) {
        LambdaQueryWrapper<SysSeqInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysSeqInfo::getSeqType, type);
        SysSeqInfo sysSeqInfo = this.getOne(wrapper);
        if (sysSeqInfo == null) {
            return "";
        }
        Long id;
        if (StringUtils.isEmpty(sysSeqInfo.getCurSeqNo())) {
            id = Long.valueOf(sysSeqInfo.getMinValue());
        } else {
            id = Long.valueOf(sysSeqInfo.getCurSeqNo()) + 1;
        }
        sysSeqInfo.setCurSeqNo(String.valueOf(id));
        this.saveOrUpdate(sysSeqInfo);
        return String.valueOf(id);
    }
}
